package com.dhnsoft.education.service.edu.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhnsoft.education.entity.edu.EduCourse;
import com.dhnsoft.education.entity.edu.EduCourseDescription;
import com.dhnsoft.education.entity.edu.frontVo.CourseFrontVo;
import com.dhnsoft.education.entity.edu.frontVo.CourseWebVo;
import com.dhnsoft.education.entity.edu.vo.CourseInfoVo;
import com.dhnsoft.education.entity.edu.vo.CoursePublishVo;
import com.dhnsoft.education.entity.edu.vo.SubjectNestedVo;
import com.dhnsoft.education.handle.EduException;
import com.dhnsoft.education.mapper.edu.EduCourseMapper;
import com.dhnsoft.education.service.edu.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 代浩楠
 * @since 2022-02-19
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {


    @Autowired
    private EduCourseDescriptionService descriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduSubjectService eduSubjectService;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int result = baseMapper.insert(eduCourse);
        if(result==0){
            throw new EduException(20001,"添加课程信息失败");
        }
        String cid = eduCourse.getId();
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(cid);
        descriptionService.save(courseDescription);
        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        EduCourseDescription courseDescription = descriptionService.getById(courseId);
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        courseInfoVo.setDescription(courseDescription.getDescription());
        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int flag = baseMapper.updateById(eduCourse);
        if(flag<=0){
            throw new EduException(20001,"修改课程信息失败");
        }
        EduCourseDescription description = new EduCourseDescription();
        description.setDescription(courseInfoVo.getDescription());
        descriptionService.updateById(description);
    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    public void removeCourse(String courseId) {
        //删除小节
        eduVideoService.removeVideoByCourseId(courseId);

        //删除章节
        eduChapterService.removeChapterByCourseId(courseId);

        //删除描述
        QueryWrapper<EduCourseDescription> eduCourseDescriptionQueryWrapper = new QueryWrapper<>();
        eduCourseDescriptionQueryWrapper.eq("id",courseId);
        descriptionService.remove(eduCourseDescriptionQueryWrapper);
        //删除课程
        QueryWrapper<EduCourse> eduCourseQueryWrapper = new QueryWrapper<>();
        eduCourseQueryWrapper.eq("id",courseId);
        int delete = this.baseMapper.delete(eduCourseQueryWrapper);
        if(delete == 0 ){
            throw new EduException(20001,"删除失败");
        }
    }

    @Override
    @Cacheable(key ="'selectCourseList'",value = "banner")
    public List<EduCourse> getHotCourse() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("view_count");
        wrapper.last("limit 8");
        List<EduCourse> eduCourseList = this.list(wrapper);
        return eduCourseList;
    }
//
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> coursePage, CourseFrontVo courseFrontVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){//一级分类
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){//二级分类
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){//关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())){//价格
            wrapper.orderByDesc("price");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){//最新，创建时间
            wrapper.orderByDesc("gmt_create");
        }
        baseMapper.selectPage(coursePage, wrapper);
        //把分页的数据获取出来返回一个map集合
        HashMap<String, Object> map = new HashMap<>();
        //总记录数
        long total = coursePage.getTotal();
        //当前页
        long current = coursePage.getCurrent();
        //每页记录数
        long size = coursePage.getSize();
        //查询到的对象
        List<EduCourse> teacherList = coursePage.getRecords();
        //总页数
        long pages = coursePage.getPages();
        //是否有上一页
        boolean hasPrevious = coursePage.hasPrevious();
        //是否有下一页
        boolean hasNext = coursePage.hasNext();

        //将数据封装到map中返回
        map.put("total",total);
        map.put("current",current);
        map.put("size",size);
        map.put("list",teacherList);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        map.put("pages",pages);
        return map;
    }
    //根据课程id，编写sql语句查询课程信息
    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {

        return baseMapper.getBaseCourseInfo(courseId);
    }

    @Override
    public List<SubjectNestedVo> getSubjectNestedList(Long count) {
        List<SubjectNestedVo> subjectList=eduSubjectService.getAllSubject(count);
        return subjectList;
    }


}
